package org.example.ExperimentTwo;
import org.apache.commons.cli.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.regex.Pattern;

public class CommandLine {

    public static void main(String[] args) {
        Options options = new Options();
        options.addOption("r", true, "Generate a random number within the specified range (default range: 0-100)");
        options.addOption("d", false, "Display the current date and time");
        options.addOption("p", true, "Check if a phone number is valid");
        options.addOption("a", true, "Check if a string is a palindrome");

        CommandLineParser parser = new DefaultParser();
        try {
            org.apache.commons.cli.CommandLine cmd = parser.parse(options, args);

            if (cmd.hasOption("r")) {
                String range = cmd.getOptionValue("r");
                generateRandomNumber(range);
            }

            if (cmd.hasOption("d")) {
                displayCurrentDateTime();
            }

            if (cmd.hasOption("p")) {
                String phoneNumber = cmd.getOptionValue("p");
                checkPhoneNumber(phoneNumber);
            }

            if (cmd.hasOption("a")) {
                String inputString = cmd.getOptionValue("a");
                checkPalindrome(inputString);
            }
        } catch (ParseException e) {
            System.err.println("Error parsing command line arguments: " + e.getMessage());
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("CommandLineParserExample", options);
        }
    }

    private static void generateRandomNumber(String range) {
        int min = 0;
        int max = 100;
        if (range != null) {
            String[] bounds = range.split("-");
            if (bounds.length == 2) {
                min = Integer.parseInt(bounds[0]);
                max = Integer.parseInt(bounds[1]);
            }
        }
        Random random = new Random();
        int randomNumber = random.nextInt(max - min + 1) + min;
        System.out.println("Random number within the range " + min + "-" + max + ": " + randomNumber);
    }

    private static void displayCurrentDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String currentDateAndTime = sdf.format(new Date());
        System.out.println("Current date and time: " + currentDateAndTime);
    }

    private static void checkPhoneNumber(String phoneNumber) {
        // Regular expression to validate phone numbers
        String regex = "^1[3-9]\\d{9}$";
        boolean isValid = Pattern.matches(regex, phoneNumber);
        System.out.println("PhoneNumber " + phoneNumber + " valid? " + isValid);
    }

    private static void checkPalindrome(String inputString) {
        String reversedString = new StringBuilder(inputString).reverse().toString();
        boolean isPalindrome = inputString.equalsIgnoreCase(reversedString);
        System.out.println("String \"" + inputString + "\" a palindrome? " + isPalindrome);
    }
}
